package net.zjitc.homework.homework11.jdbc.book;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;

/**
 * @Author admin
 * @Date 2019/4/26 9:06
 */
public class Test {
    static BookDaoDB dao = new BookDaoDB();
    static Scanner scannerInt = new Scanner(System.in);
    static Scanner scannerString = new Scanner(System.in);
    static Scanner scannerDouble = new Scanner(System.in);

    public static void main(String[] args) {
        while (true) {
            System.out.println("1--保存图书信息");
            System.out.println("2--查询图书信息");
            System.out.println("3--更新图书信息");
            System.out.println("4--删除图书信息");
            System.out.println("5--退出");
            System.out.println("请输入你的选择：");
            int choose = scannerInt.nextInt();
            switch (choose) {
                case 1:
                    insertUI();
                    break;
                case 2:
                    selectUI();
                    break;
                case 3:
                    updateUI();
                    break;
                case 4:
                    deleteUI();
                    break;
                case 5:
                    exitUI();
                    break;
                default:
                    System.out.println("输入有误！");
                    break;
            }
        }
    }

    private static void exitUI() {
        dao.exit();
        System.out.println("退出系统");
        System.exit(0);
    }

    private static void deleteUI() {
        System.out.println("------删除界面------");
        System.out.println("请输入要删除的图书名称：");
        String name = scannerString.nextLine();
        Book book = dao.query(name);
        if (book == null) {
            System.out.println("你要删除的图书不存在");
        } else {
            int s = dao.delete(book);
            if (s > 0) {
                System.out.println("删除成功");
            } else {
                System.out.println("删除失败");
            }
        }
    }

    private static void updateUI() {
        System.out.println("------更新界面------");
        System.out.println("请输入你要修的图书名称：");
        String name = scannerString.nextLine();
        Book book = dao.query(name);
        if (book == null) {
            System.out.println("你要修改的图书不出在");
        } else {
            System.out.println("请输入修改后的图书名称：");
            String name1 = scannerString.nextLine();
            System.out.println("请输入修改后的出版社：");
            String press = scannerString.nextLine();
            System.out.println("请输入修改后的作者：");
            String author = scannerString.nextLine();
            System.out.println("请输入修改后的价格：");
            double price = scannerDouble.nextDouble();
            Book book1 = new Book(book.getId(), name1, press, author, price);
            int s = dao.update(book1);
            if (s > 0) {
                System.out.println("更新成功");
            } else {
                System.out.println("更新失败");
            }
        }
    }

    private static void selectUI() {
        System.out.println("------所有图书信息------");
        System.out.println("编号\t名称\t出版社\t作者\t价格");
        /*    dao.select();*/
        ArrayList<Book> arrayList = dao.selectAll();
        Iterator<Book> iterator = arrayList.iterator();
        while (iterator.hasNext()) {
            Book book = (Book) iterator.next();
            System.out.println(book.getId() + "\t" + book.getName() + "\t" + book.getPress() + "\t" + book.getAuthor() + "\t" + book.getPrice());

        }

    }

    private static void insertUI() {
        System.out.println("------添加界面------");
        System.out.println("请输入图书名称：");
        String name = scannerString.nextLine();
        System.out.println("请输入图书出版社：");
        String press = scannerString.nextLine();
        System.out.println("请输入图书作者：");
        String author = scannerString.nextLine();
        System.out.println("请输入图书价格：");
        double price = scannerDouble.nextDouble();
        Book book = new Book(name, press, author, price);
        int s = dao.insert(book);
        if (s > 0) {
            System.out.println("添加成功");
        } else {
            System.out.println("图书名称重复，添加失败");
        }
    }
}
